package com.dingup.provider.controller.feedback;

import com.dingup.api.base.HttpResContent;
import com.dingup.api.bean.feign.paike.feedback.PkLessonInfoFeignBean;
import com.dingup.api.bean.page.PageData;
import com.dingup.api.model.classInfo.ClassInfo;
import com.dingup.provider.feign.client.paike.feedback.PaikeFeedbackFeignClient;
import com.dingup.provider.mapper.ClassInfoMapper;
import com.suyeer.basic.util.BHttpResUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/10/27.
 */
@RestController()
@RequestMapping("/feedback")
public class FeedbackController {

    @Autowired
    PaikeFeedbackFeignClient paikeFeignClient;
    @Autowired
    ClassInfoMapper classInfoMapper;

    @RequestMapping(value = "/get_feedback_list_by_studentId_and_class_ids.do", method = RequestMethod.POST)
    public void getFeedbackList(HttpServletRequest request,
                                HttpServletResponse response,
                                String studentId,
                                Integer lessonId,
                                Integer page,
                                Integer rows) {
        HttpResContent<PageData> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取学生的课堂报告");
        try {
            List<ClassInfo> classInfos = classInfoMapper.getClassByLessonId(lessonId);
            List<Integer> classIds = new ArrayList<>();
            if (!CollectionUtils.isEmpty(classInfos)) {
                classIds = classInfos.stream()
                        .map(ClassInfo::getId).collect(Collectors.toList());
            }
            PkLessonInfoFeignBean bean = new PkLessonInfoFeignBean();
            bean.setPage(page);
            bean.setRows(rows);
            bean.setStudentId(studentId);
            bean.setClassIds(classIds);
            PageData res = paikeFeignClient.getFeedbackListByStudentIdAndClassIds(bean);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
}
